package com.sfh.cpc.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.sfh.cpc.R;
import com.sfh.cpc.home.menu.HomeFragment;
import com.sfh.cpc.home.menu.WelfareFragment;
import com.sfh.cpc.home.menu.MassFragment;
import com.sfh.cpc.com.nav.NavigationButton;
import com.sfh.cpc.home.menu.UserFragment;

import java.util.List;


/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/4/27
 */
public class NavFragment extends Fragment implements View.OnClickListener {


    private NavigationButton mNavHome;
    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment_nav, container, false);
        mNavHome = view.findViewById(R.id.nav_item_home);
        NavigationButton mNavFl = view.findViewById(R.id.nav_item_welfare);
        NavigationButton mNavMass = view.findViewById(R.id.nav_item_mass);
        NavigationButton mNavUser = view.findViewById(R.id.nav_item_user);


        mNavHome.init(R.drawable.file_home_tab_icon_home, HomeFragment.class);
        mNavFl.init(R.drawable.file_home_tab_icon_fl, WelfareFragment.class);
        mNavMass.init(R.drawable.file_home_tab_icon_mass, MassFragment.class);
        mNavUser.init(R.drawable.file_home_tab_icon_user, UserFragment.class);

        mNavHome.setOnClickListener(this);
        mNavFl.setOnClickListener(this);
        mNavMass.setOnClickListener(this);
        mNavUser.setOnClickListener(this);
        return view;
    }



    @Override
    public void onClick(View v) {
        NavigationButton nav = (NavigationButton) v;
        doSelect(nav);
    }

    private void doSelect(NavigationButton newNavButton) {

        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void onReselect(NavigationButton navigationButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navigationButton);
        }
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }

    public interface OnNavigationReselectListener {
        void onReselect(NavigationButton navigationButton);
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;

        // do clear
        clearOldFragment();
        // do select first
        doSelect(mNavHome);
    }

    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0) {
            return;
        }
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this && fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit) {
            transaction.commitNow();
        }
    }
}
